﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace LuDragon.IService
{
    /// <summary>
    /// EF基础服务接口
    /// </summary>
    public interface IBaseService<T>
    {
        #region 读     
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where">条件</param>
        /// <returns></returns>
        T GetModel(Expression<Func<T, bool>> where);
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderby">排序字段</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns></returns>
        IEnumerable<T> GetAll(string sortName = null, string sortOrder = "ASC");
        /// <summary>
        /// 分页获取所有
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="count">总记录</param>
        /// <param name="orderby">排序字段</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns></returns>
        IEnumerable<T> GetAllWithPage(int pageIndex, int pageSize, out int count, string sortName, string sortOrder = "ASC");

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="where">条件</param>
        /// <param name="orderby">排序字段</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns></returns>
        IEnumerable<T> GetByWhere(Expression<Func<T, bool>> where, string sortName = null, string sortOrder = "ASC");
        /// <summary>
        /// 条件分页查询
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="count">总计录</param>
        /// <param name="orderby">排序字段</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns></returns>
        IEnumerable<T> GetByWhereWithPage(Expression<Func<T, bool>> where, int pageIndex, int pageSize, out int count, string sortName, string sortOrder = "ASC");
        /// <summary>
        /// SQL查询
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        IEnumerable<T> GetBySql(string sql, SqlParameter[] parameters = null);
        #endregion

        #region 写
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        T Add(T entity);
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        bool Delete(T entity);
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        bool Update(T entity);
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        int ExcuteSql(string sql, SqlParameter[] parameters = null);
        #endregion

    }
}
